﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frn_TTWithContract : Form
    {
        private int m_nSelectFeeTTLoanAndFeeTabID = 0;
        private int m_nSelectFeeTTLoanContractTabID = 0;

        private DGBY.BLL.FeeTTLoanContractTab m_bllFeeTTLoanContractTab = new DGBY.BLL.FeeTTLoanContractTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        public Frn_TTWithContract()
        {
            InitializeComponent();
        }

        public Frn_TTWithContract(int nSelectFeeTTLoanAndFeeTabID)
        {
            m_nSelectFeeTTLoanAndFeeTabID = nSelectFeeTTLoanAndFeeTabID;
            InitializeComponent();
        }

        private void ShowContractInfoByID(int nSelectFeeTTLoanAndFeeTabID)
        {
            DataSet ds = m_bllFeeTTLoanContractTab.GetAllContractInfo("FeeTTLoanContractTab.FeeTTLoanAndFeeRegDetailInfoTabID = " + nSelectFeeTTLoanAndFeeTabID.ToString());
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewList.DataSource = ds.Tables[0];
                dataGridViewList.Columns[0].Visible = false;
                dataGridViewList.Columns[1].Visible = false;
            }
        }

        private void Frn_TTWithContract_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = ds.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.SelectedIndex = 0;
            }
            ShowContractInfoByID(m_nSelectFeeTTLoanAndFeeTabID);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FeeTTLoanContractTab modelFeeTTLoanContractTab = new DGBY.Model.FeeTTLoanContractTab();

                modelFeeTTLoanContractTab.FeeTTLoanAndFeeRegDetailInfoTabID = m_nSelectFeeTTLoanAndFeeTabID;

                if (this.comboBoxContractNo.SelectedValue != null)
                    modelFeeTTLoanContractTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);


                m_bllFeeTTLoanContractTab.Add(modelFeeTTLoanContractTab);

                ShowContractInfoByID(m_nSelectFeeTTLoanAndFeeTabID);

                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonMod_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FeeTTLoanContractTab modelFeeTTLoanContractTabOld = m_bllFeeTTLoanContractTab.GetModel(m_nSelectFeeTTLoanContractTabID);
                if (modelFeeTTLoanContractTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.FeeTTLoanContractTab modelFeeTTLoanContractTab = new DGBY.Model.FeeTTLoanContractTab();

                modelFeeTTLoanContractTab.FeeTTLoanAndFeeRegDetailInfoTabID = m_nSelectFeeTTLoanAndFeeTabID;

                if (this.comboBoxContractNo.SelectedValue != null)
                    modelFeeTTLoanContractTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);

                modelFeeTTLoanContractTab.FeeTTLoanContractTabID = m_nSelectFeeTTLoanContractTabID;

                m_bllFeeTTLoanContractTab.Update(modelFeeTTLoanContractTab);

                ShowContractInfoByID(m_nSelectFeeTTLoanAndFeeTabID);

                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            DGBY.Model.FeeTTLoanContractTab modelFeeTTLoanContractTabOld = m_bllFeeTTLoanContractTab.GetModel(m_nSelectFeeTTLoanContractTabID);
            if (modelFeeTTLoanContractTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllFeeTTLoanContractTab.Delete(m_nSelectFeeTTLoanContractTabID);

                ShowContractInfoByID(m_nSelectFeeTTLoanAndFeeTabID);
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void dataGridViewList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strFeeTTLoanContractTabID = this.dataGridViewList.Rows[this.dataGridViewList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectFeeTTLoanContractTabID = Convert.ToInt32(strFeeTTLoanContractTabID, 10);
            cellFeeTTLoanContractTabClick();
        }
        private void cellFeeTTLoanContractTabClick()
        {
            DGBY.Model.FeeTTLoanContractTab modelFeeTTLoanContractTab = m_bllFeeTTLoanContractTab.GetModel(m_nSelectFeeTTLoanContractTabID);
            if (modelFeeTTLoanContractTab != null)
            {
                //this.textBoxCargoBoxNo.Text = modelCargoBoxTab.CargoBoxNo;
                if (modelFeeTTLoanContractTab.PaidanRegisterTabID != null)
                    this.comboBoxContractNo.SelectedValue = modelFeeTTLoanContractTab.PaidanRegisterTabID;
            }
        }
        private void dataGridViewList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }
    }
}